package com.ruoyi.app.dyh.hxf.controller;

import com.ruoyi.app.dyh.hxf.domain.Active;
import com.ruoyi.app.dyh.hxf.service.IMyActiveService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Api("我参加的")
@RestController
@RequestMapping("/myActive")
public class MyActiveController extends BaseController {
    @Autowired
    private IMyActiveService myActiveService;

    @Autowired
    private ISysUserService userService;

    /**
     * 根据本人id查询服务队活动和暖心团活动
     *
     * @param active
     * @return
     */
    @ApiOperation("根据本人id查询服务队活动和暖心团活动")
    @GetMapping(value = "/list/{id}")
    public TableDataInfo list(Active active, @PathVariable("id") String id) throws ParseException {
        startPage();
        Long userId = getLoginUser().getUserId();
        active.setActiveUserId(userId.toString());
        List<Active> list = myActiveService.selectActiveList(active);
        List<Active> map1 = new ArrayList<>();
        List<Active> map2 = new ArrayList<>();
        List<Active> map3 = new ArrayList<>();
        for (Active active1 : list) {
            Date date = DateUtils.getNowDate();
            Date endTime = active1.getEndTime();
            Date createTime = active1.getStartTime();
            if (date.after(endTime)) {
                active1.setStatus(2);
                map1.add(active1);
                myActiveService.updateMyActiveByIds(active1);

            }
            if (date.before(endTime)&& date.after(createTime)) {
                active1.setStatus(1);
                map2.add(active1);
                myActiveService.updateMyActiveByIds(active1);
            }
            if (date.before(createTime)) {
                active1.setStatus(0);
                map3.add(active1);
                myActiveService.updateMyActiveByIds(active1);
            }
        }
        if (id.contains("0")) {
            return getDataTable(list);
        } else if (id.contains("1")) {
            return getDataTable(map3);
        } else if (id.contains("2")) {
            return getDataTable(map2);
        } else return getDataTable(map1);
    }

    /**
     * 删除参加的服务队活动或者暖心团活动
     *
     * @param activeId
     * @return
     */
    @ApiOperation("删除参加的服务队活动或者暖心团活动")
    @Log(title = "删除参加的服务队活动或者暖心团活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activeId}")
    public AjaxResult remove(@PathVariable("activeId") String activeId) {
        Long userId = getLoginUser().getUserId();
        StringBuffer sysUserSb = new StringBuffer();
        SysUser sysUser = userService.selectUserById(userId);
        String[] UserActiveIds = sysUser.getActiveIds().split(",");
        for (int i = 0; i < UserActiveIds.length; i++) {
            if (!(Integer.parseInt(UserActiveIds[i]) == Integer.parseInt(activeId))) {
                sysUserSb.append(UserActiveIds[i] + ",");
            }
        }
        if (sysUserSb.length() == 0) {
            sysUser.setActiveIds("");
        } else {
            sysUser.setActiveIds(sysUserSb.substring(0, sysUserSb.length() - 1));
        }
        myActiveService.updateUser(sysUser);

        StringBuffer activeSb = new StringBuffer();
        Active active = myActiveService.selectActiveById(activeId);
        String[] ActiveUserIds = active.getActiveUserId().split(",");
        for (int i = 0; i < ActiveUserIds.length; i++) {
            if (!(Integer.parseInt(ActiveUserIds[i]) == userId)) {
                activeSb.append(ActiveUserIds[i] + ",");
            }
        }
        if (activeSb.length() == 0) {
            active.setActiveUserId("");
        } else {
            active.setActiveUserId(activeSb.substring(0, activeSb.length() - 1));
        }
        return toAjax(myActiveService.updateMyActiveByIds(active));
    }

    /**
     * 查询单个活动详细信息
     *
     * @param activeId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('sadPatientPool:Info:list')")
    @ApiOperation("查询单个活动详细信息")
    @GetMapping(value = "/{activeId}")
    public AjaxResult getInfo(@PathVariable("activeId") String activeId) {
        Long userId = getLoginUser().getUserId();
        int category=myActiveService.selectCategoryById(userId,activeId);
        AjaxResult ajax = AjaxResult.success();
        Active active = myActiveService.selectActiveById(activeId);
        for (int i = 0; i < active.getActiveUserId().split(",").length; i++) {
            if(active.getActiveUserId().split(",")[i].contains(userId.toString())){
                ajax.put("join","已进入");break;
            }else {
                ajax.put("join","退出");
            }
        }
        String activeUserId = active.getActiveUserId();
        String[] split = activeUserId.split(",");
        List<SysUser> userList=new ArrayList();
        for (int i = 0; i < split.length; i++) {
            userList.add(userService.selectUserById(Long.valueOf(split[i])));
        }
        ajax.put("activeInformation", active);
        ajax.put("activeUserList", userList);
        if(category>0){
            ajax.put("category","管理员");
        }else
            ajax.put("category","成员");

        return ajax;
    }
}
